package com.yao.servlet;

import com.yao.utils.CookieUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author YAO
 * @create 2021-05-07 16:23
 */
@WebServlet(name = "CookieServlet", value = "/cookieServlet")
public class CookieServlet extends BaseServlet{

    protected void createCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.创建Cookie对象
        Cookie cookie = new Cookie("key1", "value1");
        //2.通知客户端保存cookie
        resp.addCookie(cookie);

        //1.创建Cookie对象
        Cookie cookie2 = new Cookie("key2", "value2");
        //2.通知客户端保存cookie
        resp.addCookie(cookie2);

        resp.getWriter().write("Cookie创建成功");
    }

    protected void getCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie[] cookies = req.getCookies();

        Cookie iWantCookie = CookieUtils.findCookie("key2",cookies);

        for (Cookie cookie : cookies) {
            //getName()方法返回cookie的key
            resp.getWriter().write("Cookie[" + cookie.getName() + "=" + cookie.getValue() + "]<br/>");
        }

        //如果不等于null,说明赋过值，也就是找到了需要的cookie
        if(iWantCookie != null){
            resp.getWriter().write("找到了需要的Cookie");
        }
    }

    protected void updateCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        方案一：
//        1、先创建一个要修改的同名（指的就是 key）的 Cookie 对象
//        2、在构造器，同时赋于新的 Cookie 值。
        Cookie cookie = new Cookie("key1", "newValue1");
//        3、调用 response.addCookie( Cookie );  通知客户端保存修改
        resp.addCookie(cookie);

        resp.getWriter().write("key1的Cookie已经修改好");

//        方案二：
//        1、先查找到需要修改的 Cookie 对象
        Cookie cookie2 = CookieUtils.findCookie("key2", req.getCookies());
        if(cookie2 != null){
//        2、调用 setValue()方法赋于新的 Cookie 值。
            cookie2.setValue("newValue2");
//        3、调用 response.addCookie()通知客户端保存修改
            resp.addCookie(cookie2);
            resp.getWriter().write("key2的Cookie已经修改好");
        }



    }

    protected void defaultLife(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie = new Cookie("defaultLife", "defaultLife");
        //设置存活时间
        // -1表示浏览器关闭，cookie被删除
        cookie.setMaxAge(-1);
        resp.addCookie(cookie);
    }

    protected void deleteNow(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //先找到你要删除的cookie对象
        Cookie cookie = CookieUtils.findCookie("key1", req.getCookies());
        if(cookie != null){
            //调用setMaxAge(0)  表示马上删除
            cookie.setMaxAge(0);
            //调用resp.addCookie(cookie)
            resp.addCookie(cookie);

            resp.getWriter().write("key1已经被删除");
        }
    }

    protected void life3600(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie = new Cookie("life3600", "life3600");
        //设置cookie一小时后被删除
        cookie.setMaxAge(60*60);
        resp.addCookie(cookie);
        resp.getWriter().write("已经创建了一个存活一小时的cookie");
    }

    protected void testPath(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie = new Cookie("path1", "path1");
        // getContextPath() ==> 得到工程路径
        cookie.setPath(req.getContextPath() + "/abc");
        resp.addCookie(cookie);
        resp.getWriter().write("创建了一个带有path路径的Cookie");
    }
}
